﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CsharpCodeGenerator
{
    public class CsharpHelper
    {
        public int MyProperty { get; set; }

        #region DalClass
        public static string DalClass
        {
            get
            {
                return @"using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FAP.DataAccess.DAL
{{
    public class {0}
    {{
private {1} string m_trConnectionString = ConfigurationManager.ConnectionStrings[""ConnectionString""].ConnectionString;
";
            }
        }
        #endregion

        #region BllClass
        public static string BllClass
        {
            get
            {
                return @"using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace FAP.DataAccess.BLL
{{
    public class {0}
    {{";
            }
        }
        #endregion

        #region DalSelectAll
        public static string DalSelectAll
        {
            get
            {
                return @"       #region {1}
        public {0}SqlDataReader {1}(string strSearch="""",int top = 1, int bottom = 20, SqlTransaction ConnectionString = null)
        {{
            if (ConnectionString != null)
                return SqlHelper.ExecuteReader(ConnectionString, ""{2}"",strSearch,top,bottom);
            return SqlHelper.ExecuteReader(m_trConnectionString, ""{2}"",strSearch,top,bottom);
        }}
        #endregion";
            }
        }
        #endregion

        #region DalSelectRow
        public static string DalSelectRow
        {
            get
            {
                return @"       #region {1}
        public {0}SqlDataReader {1}({2}, SqlTransaction ConnectionString = null)
        {{
            if (ConnectionString != null)
                return SqlHelper.ExecuteReader(ConnectionString, ""{1}"", {3});
            return SqlHelper.ExecuteReader(m_trConnectionString, ""{1}"", {3});
        }}
        #endregion";
            }
        }
        #endregion

        #region DalSelectRowFK
        public static string DalSelectRowFK
        {
            get
            {
                return @"       #region {1}
        public {0}SqlDataReader {1}({2}, string strSearch="""",int top = 1, int bottom = 20, SqlTransaction ConnectionString = null)
        {{
            if (ConnectionString != null)
                return SqlHelper.ExecuteReader(ConnectionString, ""{1}"", {3},strSearch,top,bottom);
            return SqlHelper.ExecuteReader(m_trConnectionString, ""{1}"", {3},strSearch,top,bottom);
        }}
        #endregion";
            }
        }
        #endregion

        #region DalInsertRow
        public static string DalInsertRow
        {
            get
            {
                return @"       #region {1}
        public {0}void {1}({2}, SqlTransaction ConnectionString = null)
        {{
            if (ConnectionString != null)
            {{
                SqlHelper.ExecuteNonQuery(ConnectionString, ""{3}"", {4});
                return;
            }}
            SqlHelper.ExecuteNonQuery(m_trConnectionString, ""{3}"", {4});
        }}
        #endregion";
            }
        }
        #endregion

        #region DalUpdateRow
        public static string DalUpdateRow
        {
            get
            {
                return @"       #region {1}
        public {0}void {1}({2}, SqlTransaction ConnectionString = null)
        {{
            if (ConnectionString != null)
            {{
                SqlHelper.ExecuteNonQuery(ConnectionString, ""{3}"", {4});
                return;
            }}
            SqlHelper.ExecuteNonQuery(m_trConnectionString, ""{3}"", {4});
        }}
        #endregion";
            }
        }
        #endregion

        #region DalDeleteRow
        public static string DalDeleteRow
        {
            get
            {
                return @"       #region {1}
        public {0}void {1}({2}, SqlTransaction ConnectionString = null)
        {{
            if (ConnectionString != null)
            {{
                SqlHelper.ExecuteNonQuery(ConnectionString, ""{3}"", {4});
                return;
            }}
            SqlHelper.ExecuteNonQuery(m_trConnectionString, ""{3}"", {4});
        }}
        #endregion";
            }
        }
        #endregion

        #region BllProperies
        public static string BllProperies
        {
            get
            {
                return @"        public {0} {1} {{ get; set; }}{2}";
            }
        }
        #endregion

        #region BllProperiesEntity
        public static string BllProperiesEntity
        {
            get
            {
                return @"        public {0} {1} {{ get{{{3}}} }}{2}";
            }
        }
        #endregion

        #region BllSelectAll
        public static string BllSelectAll
        {
            get
            {
                return @"
        #region {2}
        public {0}List<{1}> {2}(string strSearch="""",int top = 1, int bottom = 20, SqlTransaction ConnectionString = null)
        {{
            IDataReader dr = FAP.DataAccess.DAL.{1}.{2}(strSearch,top,bottom,ConnectionString);
            List<{1}> {1}List = new List<{1}>();
            while(dr.Read())
            {{
                {1} my{1} = new {1}
                {{
{3}
                }};
                {1}List.Add(my{1});
        }}
        dr.Close();
        dr.Dispose();
        return {1}List;
        }}
        #endregion";
            }
        }
        #endregion

        #region BllSelectByForeignKey
        public static string BllSelectByForeignKey
        {
            get
            {
                return @"
        #region {2}
        public {0}List<{1}> {2}({5} {4},string strSearch="""",int top = 1, int bottom = 20, SqlTransaction ConnectionString = null)
        {{
            IDataReader dr = FAP.DataAccess.DAL.{1}.{2}({4},strSearch,top,bottom,ConnectionString);
            List<{1}> {1}List = new List<{1}>();
            while(dr.Read())
            {{
                {1} my{1} = new {1}
                {{
{3}
                }};
                {1}List.Add(my{1});
        }}
        dr.Close();
        dr.Dispose();
        return {1}List;
        }}
        #endregion";
            }
        }
        #endregion

        #region BllSelectByForeignKeyAll
        public static string BllSelectByForeignKeyAll
        {
            get
            {
                return @"
        #region {2}
        public {0}List<{1}> {2}({5},string strSearch="""",int top = 1, int bottom = 20, SqlTransaction ConnectionString = null)
        {{
            IDataReader dr = FAP.DataAccess.DAL.{1}.{2}({4},strSearch,top,bottom,ConnectionString);
            List<{1}> {1}List = new List<{1}>();
            while(dr.Read())
            {{
                {1} my{1} = new {1}
                {{
{3}
                }};
                {1}List.Add(my{1});
        }}
        dr.Close();
        dr.Dispose();
        return {1}List;
        }}
        #endregion";
            }
        }
        #endregion

        #region BllSelectRow
        public static string BllSelectRow
        {
            get
            {
                return @"        #region {2}
        public {0}{1} {2}({3}, SqlTransaction ConnectionString = null)
        {{
            IDataReader dr = FAP.DataAccess.DAL.{1}.{2}({6}, ConnectionString);
            {1} {4} = null;

            if(dr.Read())
            {{
                {4}= new {1}
                {{
{5}
                }};
            }}
        dr.Close();
        dr.Dispose();
        return {4};
        }}
        #endregion";
            }
        }
        #endregion

        #region BllInsertRow
        public static string BllInsertRow
        {
            get
            {
                return @"        #region {1}
        public {0}void {1}({2}, SqlTransaction ConnectionString = null)
        {{
            FAP.DataAccess.DAL.{4}.{1}({3}, ConnectionString);
        }}
        #endregion";
            }
        }
        #endregion

        #region BllUpdateRow
        public static string BllUpdateRow
        {
            get
            {
                return @"        #region {1}
        public {0}void {1}({2}, SqlTransaction ConnectionString = null)
        {{
            FAP.DataAccess.DAL.{4}.{1}({3}, ConnectionString);
        }}
        #endregion";
            }
        }
        #endregion

        #region BllDeleteRow
        public static string BllDeleteRow
        {
            get
            {
                return @"        #region {1}
        public {0}void {1}({2}, SqlTransaction ConnectionString = null)
        {{
            FAP.DataAccess.DAL.{4}.{1}({3},ConnectionString);
        }}
        #endregion";
            }
        }
        #endregion

        #region ConvertSqlToCsharp
        public static string ConvertSqlToCsharp(string DataType)
        {
            string result = string.Empty;

            switch (DataType)
            {
                case "char":
                case "varchar":
                case "nchar":
                case "nvarchar":
                case "text":
                case "ntext":
                    {
                        result = "string";
                        break;
                    }
                case "bit":
                    {
                        result = "bool";
                        break;
                    }
                case "int":
                    {
                        result = "int";
                        break;
                    }
                case "tinyint":
                    {
                        result = "byte";
                        break;
                    }
                case "bigint":
                    {
                        result = "long";
                        break;
                    }
                case "smallint":
                    {
                        result = "short";
                        break;
                    }

                case "datetime":
                    {
                        result = "DateTime";
                        break;
                    }
                case "float":
                    {
                        result = "float";
                        break;
                    }
                case "real":
                    {
                        result = "double";
                        break;
                    }
                case "numeric":
                    {
                        result = "float";
                        break;
                    }
                default:
                    {
                        result = "string";
                        break;
                    }
            }
            return result;
        }
        public static string CsharpDefault(string DataType)
        {
            string result = string.Empty;

            switch (DataType)
            {
                case "char":
                case "varchar":
                case "nchar":
                case "nvarchar":
                case "text":
                case "string":
                    {
                        result = "null";
                        break;
                    }
                case "bit":
                    {
                        result = "false";
                        break;
                    }
                case "int":
                    {
                        result = "0";
                        break;
                    }
                case "tinyint":
                    {
                        result = "0";
                        break;
                    }
                case "bigint":
                    {
                        result = "0";
                        break;
                    }
                case "smallint":
                    {
                        result = "0";
                        break;
                    }

                case "datetime":
                    {
                        result = "DateTime.Now";
                        break;
                    }
                case "float":
                    {
                        result = "0";
                        break;
                    }
                case "real":
                    {
                        result = "0";
                        break;
                    }
                case "numeric":
                    {
                        result = "0";
                        break;
                    }
                default:
                    {
                        result = "null";
                        break;
                    }
            }
            return result;
        }
        #endregion
    }
}
